using System;
using System.Collections.Generic;
using System.Text;

namespace AntsMap
{
    class Solver2Exchange : Solver
    {
        private int maxiter;
        private int curiter;
        //private string tries;
        private int[,] tempsol;

        public Solver2Exchange(int[,] soln, int dim, int elem, CostMap map, int iter )
        {
            solution = soln;
            prevsol = soln;
            tempsol = soln;
            bestsol = soln;
            dimensions = dim;
            elements = elem;
            Optimal = false;
            costmap = map;
            bestcost = 1000000000; //TODO fix that silly
            maxiter = iter;
            curiter = 0;
        }


        public override int[,] SolveStep()
        {
            Random r = new Random();

            // The column we're in
            for (int i = 0; i < dimensions; i++)
            {
                // The main index to permute with in the column
                for (int j = 0; j < elements-1; j++)
                {
                    // The indices to continually swap with, down the column
                    for (int k = j+1; k < elements; k++)
                    {
                        int tval = solution[j, i];
                        solution[j, i] = solution[k, i];
                        solution[k, i] = tval;
                        
                        if (costmap.Cost(solution) < bestcost)
                        {
                            bestcost = costmap.Cost(solution);
                            Array.Copy(solution, bestsol, elements * dimensions);
                            Array.Copy(solution, prevsol, elements * dimensions);
                            //tries += "-found GOOD-";
                            return solution;
                        }
                        else
                        {
                            Array.Copy(prevsol, solution, elements * dimensions);
                           // tries += " (RESET) ";
                        }

                    }
                }
            }
            /*
            int randcol = r.Next(0, costmap.Dimension);
            int randel1 = r.Next(0, costmap.Elements);
            int randel2 = r.Next(0, costmap.Elements);
            while (randel2 == randel1)
                randel2 = r.Next(0, costmap.Elements);

            int tval = solution[randel1, randcol];
            solution[randel1, randcol] = solution[ randel2, randcol];
            solution[randel2, randcol] = tval;

            if (costmap.Cost(solution) < bestcost)
            {
                bestcost = costmap.Cost(solution);
                Array.Copy(solution, bestsol, elements*dimensions);
                Array.Copy(solution, prevsol, elements * dimensions);
            }
            else
            {
                Array.Copy(prevsol, solution,elements*dimensions);
            }
           

            curiter++;
            if (curiter == maxiter)
                Optimal = true;
            */
            Optimal = true;
            return solution;

        }

        public override int[,] BestSolution()
        {
            return bestsol;
        }

        public override int BestCost()
        {
            return bestcost;
        }

        public override string Message()
        {
            //return tries;
           return "Current iteration " + curiter.ToString();
        }

    }

}
